package com.common;

/**
 * Created by sola on 2018/1/28.
 */

import org.springframework.core.convert.converter.Converter;
import org.springframework.data.domain.Pageable;

import java.util.List;
@Deprecated
public class MyPageImpl<T> extends Chunk<T> implements MyPage<T> {

    private long total;
    private Pageable pageable;
    private String content ;
    private String totalElements;

    public MyPageImpl() {

    }
    public MyPageImpl(List<T> content, long total) {
        super(content);
        this.total = !content.isEmpty() && pageable != null && (long) (pageable.getOffset() + pageable.getPageSize()) > total ? (long) (pageable.getOffset() + content.size()) : total;
    }

    public MyPageImpl(List<T> content, Pageable pageable, long total) {
        super(content, pageable);
        this.pageable = pageable;
        this.total = !content.isEmpty() && pageable != null && (long) (pageable.getOffset() + pageable.getPageSize()) > total ? (long) (pageable.getOffset() + content.size()) : total;
    }

    public MyPageImpl(List<T> content) {
        this(content, (Pageable) null, null == content ? 0L : (long) content.size());
    }

    @Override
    public int getTotalPages() {
        return this.getSize() == 0 ? 1 : (int) Math.ceil((double) this.total / (double) this.getSize());
    }

    @Override
    public long getTotalElements() {
        return this.total;
    }

    @Override
    public boolean hasNext() {
        return this.getNumber() + 1 < this.getTotalPages();
    }

    @Override
    public boolean isLast() {
        return !this.hasNext();
    }

    @Override
    public <S> MyPage<S> map(Converter<? super T, ? extends S> converter) {
        return new MyPageImpl(this.getConvertedContent(converter), this.pageable, this.total);
    }

    @Override
    public String toString() {
        String contentType = "UNKNOWN";
        List<T> content = this.getContent();
        if (content.size() > 0) {
            contentType = content.get(0).getClass().getName();
        }

        return String.format("Page %s of %d containing %s instances", new Object[]{Integer.valueOf(this.getNumber() + 1), Integer.valueOf(this.getTotalPages()), contentType});
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        } else if (!(obj instanceof MyPageImpl)) {
            return false;
        } else {
            MyPageImpl<?> that = (MyPageImpl) obj;
            return this.total == that.total && super.equals(obj);
        }
    }

    @Override
    public int hashCode() {
        int result = 17;
        result = result + 31 * (int) (this.total ^ this.total >>> 32);
        result += 31 * super.hashCode();
        return result;
    }
}
